package de.tabuma.pm.gadget.controller;

import de.tabuma.lib.tabumacontroller;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import de.tabuma.pm.*;
import de.tabuma.pm.gadget.entity.*;
import de.tabuma.pm.gadget.logik.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class update extends tabumacontroller {
    public update(String servlet, Hashtable hashtable) {
        super(servlet, hashtable);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        request.setAttribute(constants.gadgetActionId, "" + constants.GADGET_ACTION_UPDATE);
        String gadgetId = request.getParameter(constants.gadgetId);
        int gadgetIdInt = Integer.valueOf(gadgetId).intValue();
        Gadget gadget = GadgetFacade.get(gadgetIdInt);
        request.setAttribute(constants.gadget, gadget);
        if(request.getParameter(constants.GADGET_ACTION_UPDATE1)!=null) {
            String name = request.getParameter(constants.GADGET_NAME);
            gadget.setName(name);

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            GadgetFacade.update(gadget, user.getEmail());

            list list = new list(this.getServlet(), this.getParameter());
            list.navigate(context, request, response, out);
        }
    }
}